﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PostgreSQL
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string msg = "【方法一：判断数据OmegaTime的时间间隔，大于指定时长的就触发，证明查询效率异常。】";

            int xiansuoChaoshi = 0;

            try
            {
                xiansuoChaoshi = Convert.ToInt32(textBox_xiansuo.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("请正确输入超时判定依据，填写数字。单位是毫秒");
                return;
            }

            int errorCount = 0;

            DateTime dt1 = dateTimePicker1.Value;
            DateTime dt2 = dateTimePicker2.Value;
            using (ModelsQi.TemperatureMatrixContext dbqi = new ModelsQi.TemperatureMatrixContext())
            {
                var data = dbqi.TestData.Where(o => o.OmegaTime >= dt1 && o.OmegaTime <= dt2).ToList();

                label2.Text = $"总共查询到{data.Count}条数据";

                ModelsQi.TestDatum tempRow_Before = null;
                for (int i = 0; i < data.Count; i++)
                {

                    ModelsQi.TestDatum tempRow_Now = data[i];
                    if (tempRow_Before == null)
                    {
                        //首次进入，不进行比较
                    }
                    else
                    {
                        //第二次进入，开始比较
                        TimeSpan ts = (DateTime)tempRow_Now.OmegaTime - (DateTime)tempRow_Before.OmegaTime;
                        if (ts.TotalMilliseconds > xiansuoChaoshi)
                        {
                            if (msg == "")
                            {
                                msg = $"ID:{tempRow_Before.Id} - ID:{tempRow_Now.Id}  时长为：{ts.TotalMilliseconds}ms   omega程序时间：【old:{((DateTime)tempRow_Before.OmegaTime).ToString("yyyy-MM-dd HH:mm:ss fff")} new:{((DateTime)tempRow_Now.OmegaTime).ToString("yyyy-MM-dd HH:mm:ss fff")}】  铝盒时间：【old:{((DateTime)(tempRow_Before.CreatedOn)).ToString("yyyy-MM-dd HH:mm:ss fff")} new:{((DateTime)(tempRow_Now.CreatedOn)).ToString("yyyy-MM-dd HH:mm:ss fff")}】";
                            }
                            else
                            {
                                msg = $"{msg}\r\nID:{tempRow_Before.Id} - ID:{tempRow_Now.Id}  时长为：{ts.TotalMilliseconds}ms   omega程序时间：【old:{((DateTime)tempRow_Before.OmegaTime).ToString("yyyy-MM-dd HH:mm:ss fff")} new:{((DateTime)tempRow_Now.OmegaTime).ToString("yyyy-MM-dd HH:mm:ss fff")}】  铝盒时间：【old:{((DateTime)(tempRow_Before.CreatedOn)).ToString("yyyy-MM-dd HH:mm:ss fff")} new:{((DateTime)(tempRow_Now.CreatedOn)).ToString("yyyy-MM-dd HH:mm:ss fff")}】";
                            }
                            errorCount++;
                        }
                    }

                    tempRow_Before = data[i];
                }
            }


            textBox1.Text = msg;
            label6.Text = $"总计：{errorCount}条线索被发现，请注意核实";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string msg = "【方法二：判断数据CreateOn的时间间隔，小于1秒的就触发，证明铝盒摄像头没有稳定实时更新数据。】";

            int xiansuoChaoshi = 0;

            try
            {
                xiansuoChaoshi = Convert.ToInt32(textBox_xiansuo.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("请正确输入超时判定依据，填写数字。单位是毫秒");
                return;
            }

            int errorCount = 0;

            DateTime dt1 = dateTimePicker1.Value;
            DateTime dt2 = dateTimePicker2.Value;
            using (ModelsQi.TemperatureMatrixContext dbqi = new ModelsQi.TemperatureMatrixContext())
            {
                var data = dbqi.TestData.Where(o => o.OmegaTime >= dt1 && o.OmegaTime <= dt2).ToList();

                label2.Text = $"总共查询到{data.Count}条数据";

                ModelsQi.TestDatum tempRow_Before = null;
                for (int i = 0; i < data.Count; i++)
                {

                    ModelsQi.TestDatum tempRow_Now = data[i];
                    if (tempRow_Before == null)
                    {
                        //首次进入，不进行比较
                    }
                    else
                    {
                        //第二次进入，开始比较
                        TimeSpan ts = (DateTime)tempRow_Now.CreatedOn - (DateTime)tempRow_Before.CreatedOn;
                        if (ts.TotalMilliseconds < 1000)
                        {
                            if (msg == "")
                            {
                                msg = $"ID:{tempRow_Before.Id} - ID:{tempRow_Now.Id}  时长为：{ts.TotalMilliseconds}ms   omega程序时间：【old:{((DateTime)tempRow_Before.OmegaTime).ToString("yyyy-MM-dd HH:mm:ss fff")} new:{((DateTime)tempRow_Now.OmegaTime).ToString("yyyy-MM-dd HH:mm:ss fff")}】  铝盒时间：【old:{((DateTime)(tempRow_Before.CreatedOn)).ToString("yyyy-MM-dd HH:mm:ss fff")} new:{((DateTime)(tempRow_Now.CreatedOn)).ToString("yyyy-MM-dd HH:mm:ss fff")}】";
                            }
                            else
                            {
                                msg = $"{msg}\r\nID:{tempRow_Before.Id} - ID:{tempRow_Now.Id}  时长为：{ts.TotalMilliseconds}ms   omega程序时间：【old:{((DateTime)tempRow_Before.OmegaTime).ToString("yyyy-MM-dd HH:mm:ss fff")} new:{((DateTime)tempRow_Now.OmegaTime).ToString("yyyy-MM-dd HH:mm:ss fff")}】  铝盒时间：【old:{((DateTime)(tempRow_Before.CreatedOn)).ToString("yyyy-MM-dd HH:mm:ss fff")} new:{((DateTime)(tempRow_Now.CreatedOn)).ToString("yyyy-MM-dd HH:mm:ss fff")}】";
                            }
                            errorCount++;
                        }
                    }

                    tempRow_Before = data[i];
                }
            }


            textBox1.Text = msg;
            label6.Text = $"总计：{errorCount}条线索被发现，请注意核实";
        }
    }
}
